from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from apps.oauth.utils import SecretOauth
from .models import User
import re
def get_user_by_account(account):
    try:
        if re.match('^1[3-9]\d{9}$', account):
            user = User.objects.get(mobile=account)
        else:
            user = User.objects.get(username=account)
    except User.DoesNotExist:
        return None
    else:
        return user
class UsernameMobileAuthBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        user = get_user_by_account(username)
        if user and user.check_password(password):
            return user
def generate_verify_email_url(user):
    data_dict = {'user_id':user.id, 'email':user.email}
    secret_datas = SecretOauth().dumps(data_dict).decode()
    active_url = settings.EMAIL_ACTIVE_URL + '?token=' + secret_datas
    return active_url